<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStoreHook } from '@/store/modules/user'
import { ElLoading, ElMessage } from 'element-plus'

const ruleFormRef = ref()
const loading = ref(false)
const ruleForm = reactive({
  username: '',
  password: '',
})
const rules = reactive({
  email: [{ required: true, message: '请输入帐号', trigger: 'blur' }],
  pwd: [{ required: true, message: '请输入密码', trigger: 'blur' }],
})

const router = useRouter()
const useUserStore = useUserStoreHook()
const login = async () => {
  ruleFormRef.value.validate(valid => {
    if (valid) {
      loading.value = true
      useUserStore
        .login(ruleForm)
        .then(res => {
          console.log(res, '---')

          router.push({ path: '/' })
        })
        .finally(() => {
          loading.value = false
        })
    } else {
      ElMessage.error('验证失败')
      return false
    }
  })
}
</script>

<template>
  <div v-loading="loading" class="login-container">
    <div class="login-wrap">
      <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-position="top" class="login-form">
        <p class="welcome text-center">欢迎</p>
        <el-form-item label="帐号" prop="username" class="text-left !mb-8">
          <el-input v-model="ruleForm.username" />
        </el-form-item>
        <el-form-item label="密码" prop="password" class="relative text-left !mb-8">
          <el-input v-model="ruleForm.password" type="password" show-password />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" form-type="submit" class="w-full" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
@use './login';
</style>
